$(function(){
	pageInfo();
});

function pageInfo(pageNo){
	$.post("json/commentsInfoAction_CommentDatas",{pageNo:pageNo},function(data){
		var str="";
		var page="";
		
		$.each(data.rows,function(index,item){
			str+='<li id="singleCommentDiv'+item.text1+'" class=""><div class="comment_wrap"><div class="user_info">';
            str+='<div class="user_pic"><img width="100px" height="100px" src="images/ShowCase/53b_35.gif" /></div>';
            str+='<div class="user_name"><p>'+item.text2+'</p></div></div><div class="comment_info">';
            str+='<div class="tit"><span class="floor">'+item.text1+'楼</span><span class="c_tx3">评论时间: '+item.csdate+'</span>';
            str+='</div><table style="width:100%"><tbody><tr><td style="font-size:14px">'+replace_em(item.cscontents)+' </td>';
            str+='<td></td></tr></tbody></table></div></div></li>';
		});
		
		if(str!=""){
			page+='<div id="commentPageIndexArea" class="pagenav">';
			page+='<div class="mod_pagenav"><p class="mod_pagenav_main">';
			
			if(data.pageNo==1){
				page+='<span class="mod_pagenav_disable ">上一页</span>';
			}else{
				page+='<a href="';
				page+='javascript:pageInfo(';
				page+=data.pageNo-1;
				page+=')">';
				page+='<span class="mod_pagenav_disable ">上一页</span></a>';
			}
			
			for(var i=1;i<=data.totalPage;i++){
				if(i==data.pageNo){
					page+='<span class="current">'+i+'</span>';
				}else{
					page+='<a href="';
					page+='javascript:pageInfo(';
					page+=i;
					page+=')">';
					page+='<span class="current">'+i+'</span></a>';
				}
			}
			
			if(data.pageNo==data.totalPage){
				page+='<span class="mod_pagenav_disable ">下一页</span></p>';
			}else{
				page+='<a href="';
				page+='javascript:pageInfo(';
				page+=data.pageNo+1;
				page+=')">';
				page+='<span class="mod_pagenav_disable ">下一页</span></a></p>';
			}
			page+='<p class="mod_pagenav_option"><span class="mod_pagenav_turn">';
			page+='转到<input id="pageIndex_input2" class="textinput" type="text" value="'+data.pageNo+'">页';
			page+='<button class="bt_tx1" type="button">确 定</button>';
			page+='</span></p></div></div>';
			$("#noCommentTip").css("display","none");
		}else{
			$("#noCommentTip").css("display","block");
		}
		$("#commentListDiv").html(str);
		$("#page_wrap").html(page);
	},"json");
}

function addComment(){
	var cscontents=$("#simpleCommentEditor").val();

	if(cscontents==""){
		$("#comment_black").fadeIn("slow",function(){
			$("#comment_black").fadeOut(1000);
		});
		return;
	}
	
	$.post("json/commentsInfoAction_addComments",{cscontents:cscontents},function(data){
		data.total=parseInt( $.trim(data.total) );
		if(data.total>0){
			$("#comment_success").fadeIn("slow",function(){
				$("#comment_success").fadeOut(1000);
			});
			
			$("#simpleCommentEditor").val("");
			
            var total=parseInt(data.total-1);
            var pageSize=parseInt($.trim(data.pageSize));
            var pageNo=parseInt($.trim(total/pageSize+1));
           
            pageInfo(pageNo);
            $("#noCommentTip").css("display","none");
            // $("#comment_total").html("评论("+total+1+")");
		}else{
			$("#comment_error").fadeIn("slow",function(){
				$("#comment_error").fadeOut(1000);
				location.href="login.html";
			});
		}
	},"json");
}


//查看表情结果
function replace_em(str){
	str = str.replace(/\</g,'&lt;');
	str = str.replace(/\>/g,'&gt;');
	str = str.replace(/\n/g,'<br/>');
	str = str.replace(/\[em_([0-9]*)\]/g,'<img src="arclist/$1.gif" border="0" />');
	return str;
}